package com.laicunba.event.listener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.PaySuccessEvent;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.FinancingTargetTagDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.FinancingTargetTagService;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.TemplateService;
import com.laicunba.service.UserService;
import com.laicunba.util.StringUtil;

@Component
public class FatherActiveListener implements ApplicationListener<PaySuccessEvent> {
	private static final Logger LOG = Logger.getLogger(FatherActiveListener.class);

	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private FinancingTargetTagService financingTargetTagService;
	@Autowired
	private UserService userService;
	@Autowired
	private TemplateService templateService;
	@Autowired
	private MobileMessageService mobileMessageService;
	@Autowired
	private RegistryService registryService;

	@Override
	public void onApplicationEvent(PaySuccessEvent event) {
//		try {
//			FinancingTargetBidDO bidDO = (FinancingTargetBidDO) event.getSource();
//			RegistryDO registryDO = registryService.get(RegistryDO.class, "act.facherday.open");
//			if (bidDO != null && registryDO != null) {
//				FinancingTargetDO target = financingTargetService.get(FinancingTargetDO.class, bidDO.getFinancingTargetId());
//				FinancingTargetTagDO targetTag = financingTargetTagService.findTargetTag(bidDO.getFinancingTargetId(), "BUY_GIVE_COUPON");
//				if(target != null && "CPB".equalsIgnoreCase(bidDO.getType())) {
//					RegistryDO registry0 = registryService.get(RegistryDO.class, "u."+bidDO.getBidUserId()+"."+target.getCode()+".cpn");
//					if(registry0 == null && target.getPeriod() >= 30 && targetTag != null) {
//						CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-father-day-red-package-5");
//						if (couponDef == null) {
//							LOG.warn("父亲节活动期间，投资送现金券,但是却找不到现金券模版");
//							return;
//						}
//						cashCouponDefService.send2UserWithMessage("act-father-day-red-package-5", bidDO.getBidUserId(), false, "父亲节活动购买专属标的发放5元现金券");
//						registry0 = new RegistryDO();
//						registry0.setId("u."+bidDO.getBidUserId()+"."+target.getCode()+".cpn");
//						registry0.setVal("1");
//						registryService.create(registry0);
//						LOG.info(String.format("父亲节活动期间，用户[%s]购买标的[%s]%s元,发放5元现金券", bidDO.getBidUserId(), target.getName(), bidDO.getBuyAmount()));
//					}
//					//40岁以上用户投资，邀请人加送10元现金券
//					RegistryDO registry = registryService.get(RegistryDO.class, "u."+bidDO.getBidUserId()+".fatherday.inviter.coupon10");
//					if(registry == null && StringUtil.isNotEmpty(bidDO.getPayPid()) && StringUtil.isNotEmpty(bidDO.getPayPid().substring(6, 14))) {
//						String bidBirthDay = bidDO.getPayPid().substring(6, 14);
//						DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//						if(sdf.parse(bidBirthDay).compareTo(sdf.parse("19770101")) < 0) {
//							UserDO bidUser = userService.get(UserDO.class, bidDO.getBidUserId());
//							if(bidUser != null && bidUser.getGmtCreate().compareTo(sdf.parse("20160616"))>0 && StringUtil.isNotEmpty(bidUser.getInviteFrom())) {
//								UserDO inviter = userService.findByInviteCode(bidUser.getInviteFrom());
//								if(inviter != null) {
//									CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-father-day-red-package-10");
//									if (couponDef == null) {
//										LOG.warn("父亲节活动期间，满40岁的好友投资送现金券,但是却找不到现金券模版");
//										return;
//									}
//									cashCouponDefService.send2UserWithMessage("act-father-day-red-package-10", inviter.getId(), false, "父亲节活动期间用户年龄大于40岁，给邀请人发放10元现金券");
//									
//									registry = new RegistryDO();
//									registry.setId("u."+bidDO.getBidUserId()+".fatherday.inviter.coupon10");
//									registry.setVal("1");
//									registryService.create(registry);
//									//发送短信
//									TemplateDO templateDO = templateService.findByCodeWidthDefault("FATHER_DAY_FRIEND_BUY_CASHCOUPON");
//									Map<String, Object> paremeter = new HashMap<String, Object>();
//									paremeter.put("MobileShortNo", StringUtil.endSubstring(bidUser.getMobile(), 4));
//									paremeter.put("cashCouponName", couponDef.getAmount().toString() + "元现金券");
//									String content = templateService.simpleMerge(templateDO.getContent(), paremeter);
//									mobileMessageService.sendMsgAsync(inviter.getMobile(), content);
//									LOG.info(String.format("父亲节活动期间，用户[%s](pid:%s)购买标的[%s]%s元,给邀请人发放10元现金券", bidDO.getBidUserId(), bidDO.getPayPid(), target.getName(), bidDO.getBuyAmount()));
//								}
//							}
//						}
//					}
//				}
//			}
//		} catch (Exception e) {
//			LOG.error("父亲节发放现金券出错了", e);
//		}
	}
}
